/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.itangqi.waveloadingview.slice;

import com.itangqi.waveloadingview.ResourceTable;
import com.itangqi.waveloadingview.WaveLoadingView;
import com.itangqi.waveloadingview.utils.Logcat;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.RadioContainer;
import ohos.agp.components.Slider;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * 主入口类
 *
 * @since 2021-04-09
 */
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private static final int CIRCLE = 0;
    private static final int TRIANGLE = 1;
    private static final int SQUARE = 2;
    private static final int RECTANGLE = 3;
    private WaveLoadingView myImage;
    private Checkbox cbStart;
    private Checkbox cbResume;
    private Checkbox cbTop;
    private Checkbox cbCenter;
    private Checkbox cbBottom;

    private Slider sliderLevel;
    private Slider sliderCircleWidth;
    private Slider sliderWaveFreq;

    private int mProgressValue; // 水位
    private int circleWidth;
    private int amplitude;

    private Text waveColor1;
    private Text waveColor2;
    private Text waveColor3;
    private Text waveColor4;
    private Text waveColor5;
    private Text waveColor6;

    private Text bgColor1;
    private Text bgColor2;
    private Text bgColor3;
    private Text bgColor4;
    private Text bgColor5;
    private Text bgColor6;

    private Text boardColor1;
    private Text boardColor2;
    private Text boardColor3;
    private Text boardColor4;
    private Text boardColor5;
    private Text boardColor6;

    private Text tvShape;
    private final List<Text> waveColorTexts = new ArrayList<>();
    private final List<Text> bgColorTexts = new ArrayList<>();
    private final List<Text> boardColorTexts = new ArrayList<>();

    private CommonDialog shapeTypeDialog;
    private boolean ignoreChange;
    private int shapeType = CIRCLE;
    private RadioContainer.CheckedStateChangedListener markChangedListener;
    private Slider.ValueChangedListener valueChangedListener;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initCheckedChangedListener();
        initValueChangedListener();
        initView();
        initListener();
        initData();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void initView() {
        myImage =
                (WaveLoadingView) findComponentById(ResourceTable.Id_waveloadingview);
        cbStart = (Checkbox) findComponentById(ResourceTable.Id_cb_stop);
        cbResume = (Checkbox) findComponentById(ResourceTable.Id_cb_pause);
        cbTop = (Checkbox) findComponentById(ResourceTable.Id_cb_top);
        cbCenter = (Checkbox) findComponentById(ResourceTable.Id_cb_center);
        cbBottom = (Checkbox) findComponentById(ResourceTable.Id_cb_bottom);

        sliderLevel = (Slider) findComponentById(ResourceTable.Id_pb_level);
        sliderCircleWidth =
                (Slider) findComponentById(ResourceTable.Id_pb_circle_width);
        sliderWaveFreq = (Slider) findComponentById(ResourceTable.Id_pb_freq);

        waveColor1 = (Text) findComponentById(ResourceTable.Id_wave_color_1);
        waveColor2 = (Text) findComponentById(ResourceTable.Id_wave_color_2);
        waveColor3 = (Text) findComponentById(ResourceTable.Id_wave_color_3);
        waveColor4 = (Text) findComponentById(ResourceTable.Id_wave_color_4);
        waveColor5 = (Text) findComponentById(ResourceTable.Id_wave_color_5);
        waveColor6 = (Text) findComponentById(ResourceTable.Id_wave_color_6);

        bgColor1 = (Text) findComponentById(ResourceTable.Id_bg_color_1);
        bgColor2 = (Text) findComponentById(ResourceTable.Id_bg_color_2);
        bgColor3 = (Text) findComponentById(ResourceTable.Id_bg_color_3);
        bgColor4 = (Text) findComponentById(ResourceTable.Id_bg_color_4);
        bgColor5 = (Text) findComponentById(ResourceTable.Id_bg_color_5);
        bgColor6 = (Text) findComponentById(ResourceTable.Id_bg_color_6);

        boardColor1 = (Text) findComponentById(ResourceTable.Id_board_color_1);
        boardColor2 = (Text) findComponentById(ResourceTable.Id_board_color_2);
        boardColor3 = (Text) findComponentById(ResourceTable.Id_board_color_3);
        boardColor4 = (Text) findComponentById(ResourceTable.Id_board_color_4);
        boardColor5 = (Text) findComponentById(ResourceTable.Id_board_color_5);
        boardColor6 = (Text) findComponentById(ResourceTable.Id_board_color_6);

        tvShape = (Text) findComponentById(ResourceTable.Id_tv_shape);

        waveColorTexts.add(waveColor1);
        waveColorTexts.add(waveColor2);
        waveColorTexts.add(waveColor3);
        waveColorTexts.add(waveColor4);
        waveColorTexts.add(waveColor5);
        waveColorTexts.add(waveColor6);

        bgColorTexts.add(bgColor1);
        bgColorTexts.add(bgColor2);
        bgColorTexts.add(bgColor3);
        bgColorTexts.add(bgColor4);
        bgColorTexts.add(bgColor5);
        bgColorTexts.add(bgColor6);

        boardColorTexts.add(boardColor1);
        boardColorTexts.add(boardColor2);
        boardColorTexts.add(boardColor3);
        boardColorTexts.add(boardColor4);
        boardColorTexts.add(boardColor5);
        boardColorTexts.add(boardColor6);
    }

    private void initListener() {
        tvShape.setClickedListener(this);
        for (Text text : waveColorTexts) {
            text.setClickedListener(this);
        }
        for (Text text : bgColorTexts) {
            text.setClickedListener(this);
        }
        for (Text text : boardColorTexts) {
            text.setClickedListener(this);
        }

        // 回调处理Switch状态改变事件
        cbStart.setCheckedStateChangedListener((button, isChecked) -> {
            if (null != myImage) {
                myImage.setStart(!isChecked);
                if (!isChecked) {
                    cbResume.setChecked(false);
                    myImage.setResume(true);
                }
            }
        });
        cbResume.setCheckedStateChangedListener((button, isChecked) -> {
            if (null != myImage) {
                myImage.setResume(!isChecked);
            }
        });

        cbTop.setCheckedStateChangedListener((button, isChecked) -> {
            if (null != myImage) {
                myImage.setTopTitle(isChecked ? "Top Title" : "");
            }
        });
        cbCenter.setCheckedStateChangedListener((button, isChecked) -> {
            if (null != myImage) {
                myImage.setCenterTitle(isChecked ? "Center Title" : "");
            }
        });
        cbBottom.setCheckedStateChangedListener((button, isChecked) -> {
            if (null != myImage) {
                myImage.setBottomTitle(isChecked ? "Bottom Title" : "");
            }
        });
        sliderCircleWidth.setValueChangedListener(valueChangedListener);
        sliderLevel.setValueChangedListener(valueChangedListener);
        sliderWaveFreq.setValueChangedListener(valueChangedListener);
    }

    private void initData() {
        if (null != myImage) {
            mProgressValue = myImage.getProgressValue();
            circleWidth = (int) myImage.getBorderWidth();
            amplitude = (int) myImage.getAmplitude();
            sliderLevel.setProgressValue(mProgressValue);
            Logcat.d("mProgressValue : " + mProgressValue);
            sliderCircleWidth.setProgressValue(circleWidth);
            Logcat.d("borderWidth : " + circleWidth);
            sliderWaveFreq.setProgressValue(amplitude);
            cbStart.setChecked(!myImage.isStart());
            cbResume.setChecked(!myImage.isResume());
        }
    }

    private void initCheckedChangedListener() {
        markChangedListener = (radioContainer, which) -> {
            if (!ignoreChange) {
                shapeType = which;
                switch (which) {
                    case CIRCLE:
                        setImageShpaeType(WaveLoadingView.ShapeType.CIRCLE);
                        break;
                    case TRIANGLE:
                        setImageShpaeType(WaveLoadingView.ShapeType.TRIANGLE);
                        break;
                    case SQUARE:
                        setImageShpaeType(WaveLoadingView.ShapeType.SQUARE);
                        break;
                    case RECTANGLE:
                        setImageShpaeType(WaveLoadingView.ShapeType.RECTANGLE);
                        break;
                    default:
                        break;
                }
                destroyDialog();
            }
        };
    }

    private void setImageShpaeType(WaveLoadingView.ShapeType type) {
        if (null != myImage) {
            myImage.setShapeType(type);
        }
    }

    private void initValueChangedListener() {
        valueChangedListener = new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i, boolean b) {
                Logcat.d("i = " + i);
                switch (slider.getId()) {
                    case ResourceTable.Id_pb_level:
                        if (null != myImage) {
                            myImage.setProgressValue(i);
                        }
                        break;
                    case ResourceTable.Id_pb_circle_width:
                        if (null != myImage) {
                            myImage.setBorderWidth(i);
                        }
                        break;
                    case ResourceTable.Id_pb_freq:
                        if (null != myImage) {
                            myImage.setAmplitude(i);
                        }
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onTouchStart(Slider slider) {
                Logcat.d("===valueChangedListener=== onTouchStart");
            }

            @Override
            public void onTouchEnd(Slider slider) {
                Logcat.d("===valueChangedListener=== onTouchEnd");
            }
        };
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_tv_shape:
                createDailog();
                break;
            case ResourceTable.Id_rb_1:
            case ResourceTable.Id_rb_2:
            case ResourceTable.Id_rb_3:
            case ResourceTable.Id_rb_4:
                destroyDialog();
                break;
            default:
                if (null != myImage) {
                    for (Text text : waveColorTexts) {
                        if (text.getId() == component.getId()) {
                            myImage.setWaveColor(text.getTextColor());
                            return;
                        }
                    }
                    for (Text text : bgColorTexts) {
                        if (text.getId() == component.getId()) {
                            myImage.setWaveBgColor(text.getTextColor());
                            return;
                        }
                    }
                    for (Text text : boardColorTexts) {
                        if (text.getId() == component.getId()) {
                            myImage.setBorderColor(text.getTextColor());
                            return;
                        }
                    }
                }
                break;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (null != myImage) {
            myImage.onDestroy();
        }
    }

    private void createDailog() {
        DirectionalLayout layout =
                (DirectionalLayout) LayoutScatter.getInstance(this)
                        .parse(ResourceTable.Layout_dialog_shape_type, null, false);
        RadioContainer rcShape =
                (RadioContainer) layout.findComponentById(ResourceTable.Id_rc_shape);
        rcShape.setMarkChangedListener(markChangedListener);
        layout.findComponentById(ResourceTable.Id_rb_1).setClickedListener(this::onClick);
        layout.findComponentById(ResourceTable.Id_rb_2).setClickedListener(this::onClick);
        layout.findComponentById(ResourceTable.Id_rb_3).setClickedListener(this::onClick);
        layout.findComponentById(ResourceTable.Id_rb_4).setClickedListener(this::onClick);

        ignoreChange = true;
        rcShape.mark(shapeType);
        ignoreChange = false;

        shapeTypeDialog = new CommonDialog(getContext());
        shapeTypeDialog.setContentCustomComponent(layout)
                .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT,
                        DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                .setAlignment(LayoutAlignment.CENTER).setAutoClosable(true).show();
    }

    private void destroyDialog() {
        if (shapeTypeDialog != null) {
            shapeTypeDialog.destroy();
            shapeTypeDialog.remove();
            shapeTypeDialog.hide();
            shapeTypeDialog = null;
        }
    }
}
